package com.eagle.roomcrawler.resolver;


import com.eagle.roomcrawler.entity.RoomInfo;
import com.eagle.roomcrawler.mapper.RoomInfoMapper;
import com.eagle.roomcrawler.util.RedisUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 数据解析器
 */
@Component
public class DataResolver {

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    /**
     * 获取网页页数
     * @param document
     * @return
     */
    public static int getPageNum(Document document){
        String pageNumStr = document.select("[id=rentid_D10_01]").select("[class=txt]").text().replace("共","").replace("页","");
        if(pageNumStr == null){
            return 0;
        }
        int pageNum = Integer.parseInt(pageNumStr);
        return pageNum;
    }

    /**
     * 解析当前页面的数据
     * @param document
     */
    public void parsePageData(Document document,String rUrl){
//        RedisUtil redisUtil = new RedisUtil();
        Elements houseList = document.select("[class=houseList]").select("[class=list hiddenMap rel]");
        for(int i = 0;i<houseList.size();i++){
            Element house = houseList.get(i);
            if(house.attr("id").equals("_qrcode")){
                 continue;
            }
            RoomInfo roomInfo = new RoomInfo();
            String url = house.select("[class=img rel floatl]").select("a").attr("href");
            if(url != null && !url.equals("")){
                url = rUrl + url;
            }
            String img = house.select("[class=img rel floatl]").select("img").attr("data-src");
            String title = house.select("[class=info rel]").select("a").attr("title");
            String font = house.select("[class=font16 mt20 bold]").text().replace("�O","平方米");
            String addr = house.select("[class=gray6 mt20]").select("span").text();
            String note = house.select("[class=note colorGreen]").text();
            String price = house.select("[class=price]").text();
            String subway = house.select("[class=note subInfor]").text();
            roomInfo.setId(System.currentTimeMillis());
            roomInfo.setUrl(url);
            roomInfo.setImg(img);
            roomInfo.setTitle(title);
            roomInfo.setFont(font);
            roomInfo.setAddr(addr);
            roomInfo.setNote(note);
            roomInfo.setPrice(price);
            roomInfo.setSubway(subway);
            String key = url + "#@#" + img + "#@#" +title + "#@#" + font + "#@#" + addr + "#@#" + note + "#@#" + price + "#@#" +subway;
            System.out.println(key);
            if(!key.equals("#@##@##@##@##@##@##@#")){
                //1有数据，存入redis
                if(redisUtil.setIfAbsent(key)){
                    //2保存进elasticSearch前，先判断key是否存在
                    try {
                        roomInfoMapper.save(roomInfo);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
